package l5;

import java.util.EmptyStackException;

public class LinkedStack<T> implements Stack<T> {
    private static class Node<T> {
        T data;
        Node<T> next;

        Node(T data,Node<T> next){
            this.data = data;
            this.next = next;
        }
    }

    private Node<T> top;
    private int size;
    public LinkedStack(){
        top = null;
        size=0;
    }

    @Override
    public boolean isEmpty() {
        return top==null;
    }

    @Override
    public int size() {
        int count =0;
        Node<T> cur = top;
        while(cur!=null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public T pop() {
        if(isEmpty()){
            throw new EmptyStackException();
        }
        T data = top.data;
        top = top.next;
        size--;
        return data;
    }

    @Override
    public void push(T t) {
        top =new Node<>(t,top);
        size++;
    }

    @Override
    public T peek() {
        if(isEmpty()){
        }
        return top.data;
    }

    @Override
    public void clear() {
        top=null;
        size=0;
    }
}
